package cc.orange.adapter;

import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import cc.orange.R;
import cc.orange.entity.FriendItemInfo;

public class ChatFriendItemTwoAdapter extends BaseQuickAdapter<FriendItemInfo.DataDTO.SocWomenDTO, ChatFriendItemTwoAdapter.ChatFriendItemAdapterViewHolder> {

    private List<FriendItemInfo.DataDTO.SocWomenDTO> data;

    private OnFollowClick onFollowClick;

    /**
     *  1: 聊友页面
     *  2: 已关注
     *  3: 关注
     */
    private int type = -1;

    public ChatFriendItemTwoAdapter(@Nullable List<FriendItemInfo.DataDTO.SocWomenDTO> data, int type) {
        super(R.layout.item_chat_friend, data);
        this.data = data;
        this.type = type;
    }

    public void setOnFollowClick(OnFollowClick onFollowClick) {
        this.onFollowClick = onFollowClick;
    }

    public void updateData(List<FriendItemInfo.DataDTO.SocWomenDTO> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    protected void convert(@NonNull ChatFriendItemAdapterViewHolder helper, FriendItemInfo.DataDTO.SocWomenDTO item) {
        helper.bindData(item);
    }

    class ChatFriendItemAdapterViewHolder extends BaseViewHolder{

        TextView tvFollow, tvUserName, tvAge, tvPosition, tvContent;
        private ImageView civHead;
        private LinearLayout llPosition;

        public ChatFriendItemAdapterViewHolder(View view) {
            super(view);
            tvFollow = view.findViewById(R.id.tv_follow);
            tvAge = view.findViewById(R.id.tv_age);
            tvUserName = view.findViewById(R.id.tv_username);
            tvPosition = view.findViewById(R.id.tv_position);
            tvContent = view.findViewById(R.id.textView);
            civHead = view.findViewById(R.id.civ_head);
            llPosition = view.findViewById(R.id.ll_position);
            switch (type) {
                case 1:
                    tvFollow.setText("私聊");
                    break;
                case 2:
                    tvFollow.setText("已关注");
                    tvFollow.setTextColor(Color.parseColor("#aaaaaa"));
                    tvFollow.setBackgroundResource(R.drawable.bg_chat_unfollow);
                    break;
                case 3:
                    tvFollow.setTextColor(Color.parseColor("#FF3062aaaaaa"));
                    tvFollow.setBackgroundResource(R.drawable.bg_chat_follow);
                    tvFollow.setText("关注");
                    break;
            }
            tvFollow.setOnClickListener(view1 -> {
                Log.e("click", "点击了关注" );
                if (onFollowClick != null) {
                    onFollowClick.onFollowClick(getAdapterPosition(), tvFollow);
                }
            });
        }

        public void bindData(FriendItemInfo.DataDTO.SocWomenDTO data) {
            tvAge.setText(data.getAge()+ "");
            tvPosition.setVisibility(View.GONE);
            tvUserName.setText(data.getName());
            String socialPurpose = data.getSocialPurpose();
            if (socialPurpose != null) {
                llPosition.setVisibility(View.VISIBLE);
                tvContent.setText(socialPurpose);
            } else {
                llPosition.setVisibility(View.GONE);
            }
            Glide.with(itemView.getContext()).load(data.getHeadPhoto()).into(civHead);
        }
    }

    public interface OnFollowClick {
        void onFollowClick(int pos, View view);
    }
}
